import moment from 'moment';
/**
 * 格式化时间格式，显示为YY-MM-DD
 * @param {string} val
 * @return {*} 
 */
const dateFilter = (val: string) => {
    return val ? val.slice(0, 10) : '';
};
const timeFilter = (val: string) => {
    return val ? `${val.slice(0, 10)} ${val.slice(11, 19)}` : '';
};
const jiraUrlFilter = (val: string) => {
    const index = val.lastIndexOf('/');
    if (index >= 0) {
        return val.substring(index + 1);
    } else {
        return val;
    }
};
const labelTypeFilter = (val: string) => {
    switch (val) {
        case '1': return '系统'; break;
        case '2': return '业务需求'; break;
        case '3': return '产品需求'; break;
        default: return '系统';
    }
};
const signpostPlanPeriodFilter = (val: string) => {
    switch (val) {
        case '1': return '季度'; break;
        case '2': return '半年度'; break;
        case '3': return '年度'; break;
        default: return '';
    }
};

const lubiaoStatusFilter = (val: string) => {
    switch (val) {
        case '0': return '待推送'; break;
        case '1': return '待规划'; break;
        case '2': return '审核中'; break;
        case '3': return '执行中'; break;
        case '4': return '待验收'; break;
        case '5': return '已完成'; break;
        default: return '';
    }
};
const valToPercentFilter = (val: string) => {
    return val ? ~~(+val * 100) + '%' : val;
};
const requireReportTypeFilter = (val: string) => {
    switch (val) {
        case '1': return '业务需求';
        case '2': return '产品需求';
        case '3': return '版本';
        default: return '';
    }
};
const reportTypeFilter = (val: string) => {
    switch (val) {
        case '1': return '月';
        case '2': return '季度';
        case '3': return '半年度';
        case '4': return '年度';
        default: return '';
    }
};
const reportTimeRangeFilter = (name: string, type) => {
    const index = name.indexOf('年');
    const year = name.slice(index - 4, index);
    const str = reportTypeFilter(type);
    const lastIndex = name.indexOf(str);
    const val: any = Number(name.slice(index + 1, lastIndex));
    let startTime: any = null;
    let endTime: any = null;
    if (type === '1') {
        startTime = moment(year)
            .month(val - 1)
            .startOf('month')
            .format('YYYY-MM-DD HH:mm:ss');
        endTime = moment(year)
            .month(val - 1)
            .endOf('month')
            .format('YYYY-MM-DD HH:mm:ss');
    }
    else if (type === '2') {
        startTime = moment(year).quarter(val).startOf('quarter').format('YYYY-MM-DD HH:mm:ss');
        endTime = moment(year).quarter(val).endOf('quarter').format('YYYY-MM-DD HH:mm:ss');
    }
    else if (type === '3') {
        startTime = moment(year).quarter(val).startOf('quarter').format('YYYY-MM-DD HH:mm:ss');
        endTime = moment(year).quarter(val).endOf('quarter').format('YYYY-MM-DD HH:mm:ss');
    }
    else if (type === '4') {
        startTime = moment(year).startOf('year').format('YYYY-MM-DD HH:mm:ss');
        endTime = moment(year).endOf('year').format('YYYY-MM-DD HH:mm:ss');
    }
    return {
        startTime,
        endTime
    };
};
const daysToDHMFilter = (val: number) => {
    if (val) {
        const d = ~~val;
        val = ~~(val * 100) % 100 * 24;
        const h = ~~(val / 100);
        val = val % 100;
        const m = ~~(val * 60 / 100);
        return `${d}d:${h}h:${m}m`;
    } else {
        return 0;
    }

};
const catalogTypeFilter = (val) => {
    switch (val) {
        case '0':
            return '产品线';
        case '1':
            return '产品';
        case '2':
            return '子产品';
        case '3':
            return '版本';
        default:
            return '';
    }
};
const systemProductTypeFilter = (val) => {
    const getName = (type) => {
        switch (type) {
            case '0': return '自主研发';
            case '1': return '外包建设';
            case '2': return '品牌矩阵产品';
            case '3': return 'OBP产品';
            case '4': return '产品化运作';
            case '5': return '一般产品';
            default: return type;
        }
    };
    if (!val) {return}
    if (val.indexOf(',') >= 0) {
        let str = '';
        val.split(',').forEach((item) => {
            if (str) {
                str = str + ',' + getName(item);
            } else {
                str += getName(item);
            }
        });
        return str;
    } else {
        return getName(val);
    }
};

const systemStatusFilter = (val) => {
    switch (val) {
        case '0':
            return '待建设';
        case '1':
            return '实施中';
        case '2':
            return '服务中';
        case '3':
            return '计划下线';
        case '4':
            return '已下线';
        default:
            return val;
    }
};
const granularityFilter = (val) => {
    switch (val) {
        case '1':
            return '周';
        case '2':
            return '月';
        case '3':
            return '预发布版本';
        default:
            return val;
    }
};
const onlineStatusChangeReasonFilter = (val) => {
    switch (val) {
        case '1':
            return '同一生产变更窗口期';
        case '2':
            return '遇节假日';
        case '3':
            return '线上观察';
        case '4':
            return '等待变更流程审批';
        default:
            return val;
    }
};

const versionControlVersionTypeFilter = (val) => {
    switch (val) {
        case '1':
            return '主线版本';
        case '2':
            return '支线版本';
        case '3':
            return '预发布版本';
        case '4':
            return '测试版本';
        default:
            return '';
    }
};

const versionControlVersionStatusFilter = (val, type) => {
    if (type === '3') {
        switch (val) {
            case '1':
                return '待开始';
            case '2':
                return '预发布版本进行中';
            case '3':
                return '预发布版本已上线';
            case '4':
                return '预发布版本已终止';
            case '5':
                return '预发布版本已终止';
            default:
                return '';
        }
    } else {
        switch (val) {
            case '1':
                return '待开始';
            case '2':
                return '进行中';
            case '3':
                return '已结束';
            case '4':
                return '已终止';
            case '5':
                return '已终止';
            default:
                return '';
        }
    }
};

const versionServiceStatusFilter = (val) => {
    switch (val) {
        case '1':
            return '待建设';
        case '2':
            return '实施中';
        case '3':
            return '服务中';
        case '4':
            return '计划下线';
        case '5':
            return '已下线';
        default:
            return '';
    }
};
const secondToDay = (val) => {
    if (val) {
        if (val >= 60) {
            const h = ~~(val / 3600);
            val = val - h * 3600;
            const m = ~~(val / 60);
            val = val - m * 60;
            const s = val % 60;
            if (s === 0 && m === 0) {
                return h ? `${h}小时` : 0;
            } else if (s === 0) {
                return h ? `${h}小时${m}分` : `${m}分`;
            } else {
                return h ? `${h}小时${m}分${s}秒` : `${m}分${s}秒`;
            }
        } else {
            return `${val}秒`;
        }
    } else {
        return 0;
    }
};
import { valToNameFilter } from '@/utils/dic';
export default {
    dateFilter,
    timeFilter,
    valToNameFilter,
    jiraUrlFilter,
    labelTypeFilter,
    valToPercentFilter,
    requireReportTypeFilter,
    reportTypeFilter,
    daysToDHMFilter,
    catalogTypeFilter,
    systemProductTypeFilter,
    systemStatusFilter,
    granularityFilter,
    reportTimeRangeFilter,
    onlineStatusChangeReasonFilter,
    versionControlVersionTypeFilter,
    versionControlVersionStatusFilter,
    signpostPlanPeriodFilter,
    lubiaoStatusFilter,
    secondToDay,
    versionServiceStatusFilter
};